package code.Frame.EPFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class SIinput {
    public JPanel panel;
    private JTextField shop_name;
    private JComboBox<String> canteen_name;
    private JButton confirm;
    private JLabel c_label;

    private int status;

    private int sid;

    public boolean end = false;

    public SIinput(int s, int id)
    {
        status = s;
        sid = id;
        if (status == 1) // 修改 : 不允许修改所属食堂
        {
            c_label.setVisible(false);
            canteen_name.setVisible(false);
        }
        else
        {
            canteen_name.addItem("");
            String[] c_names;
            try
            {
                c_names = MYSQL.getAllcanteen();
                for (String name: c_names)
                    canteen_name.addItem(name);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        confirm.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String s_name = "'" + shop_name.getText() + "'";
                String c_name = "'" + canteen_name.getSelectedItem() + "'";
                if (s_name.length() == 2)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "商铺名不能为空");
                    return ;
                }
                if (s_name.length() > 32)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "商铺名不能超过30位");
                    return ;
                }
                if (status == 1) // 修改
                {
                    try
                    {
                        MYSQL.maintain("update shop set " +
                                "name = " + s_name + "\n" +
                                "where SID = " + sid
                        );
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
                else // 新增
                {
                    if (c_name.length() == 2)
                    {
                        JOptionPane.showMessageDialog(panel.getParent(), "食堂名不能为空");
                        return ;
                    }
                    if (c_name.length() > 32)
                    {
                        JOptionPane.showMessageDialog(panel.getParent(), "食堂名不能超过30位");
                        return ;
                    }
                    try
                    {
                        if (!MYSQL.exist_canteen_name(c_name))
                        {
                            JOptionPane.showMessageDialog(panel.getParent(), "食堂名不存在");
                            return ;
                        }
                    } catch (SQLException ex)
                    {
                        throw new RuntimeException(ex);
                    }
                    try
                    {
                        MYSQL.maintain("insert into shop(name, CID, UID) values( " +
                                s_name + ", " +
                                MYSQL.getCID(c_name) + ", " +
                                (Main.uid - 200000000) + ")"
                        );
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
                FrameManager.shop.refresh_table();
                end = true;
            }
        });
    }
}
